package com.ruoyi.eqmonitor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.eqmonitor.mapper.PipeMapper;
import com.ruoyi.eqmonitor.service.IPipeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Service
public class PipeServiceImpl implements IPipeService {

    @Resource
    private PipeMapper pipeMapper;

    @Override
    public List<Map<String, Object>> getPipeJsonByCode(String code) {
        String tableName = pipeMapper.getTableNameByCode(code);
        if (StrUtil.isEmpty(tableName)) {
            throw new ServiceException("数据源不存在");
        }
        String type = "";
        if (StrUtil.indexOfIgnoreCase(code, "point") != -1) type = "point";
        if (StrUtil.indexOfIgnoreCase(code, "line") != -1) type = "line";
        return pipeMapper.getPipeLineJsonByCode(tableName, type);
    }

    @Override
    public Map<String, Object> getPointByWtdh(String pipeType, String pipeWtdh) {
        String tableName = pipeMapper.getTableNameByCode(StrUtil.format("{}_POINT", pipeType).toUpperCase(Locale.ROOT));
        if (StrUtil.isEmpty(tableName)) {
            throw new ServiceException("数据源不存在");
        }
        return pipeMapper.getPipePointJsonByWtdh(tableName, pipeWtdh);
    }

    @Override
    public List<Map<String, Object>> getLineByPointWtdh(String pipeType, String pipeWtdh) {
        String tableName = pipeMapper.getTableNameByCode(StrUtil.format("{}_LINE", pipeType).toUpperCase(Locale.ROOT));
        if (StrUtil.isEmpty(tableName)) {
            throw new ServiceException("数据源不存在");
        }
        return pipeMapper.getPipeLineJsonByWtdh(tableName, pipeWtdh);
    }

    @Override
    public Map<String, Object> getLineByQddhAndZddh(String pipeType, String QDDH, String ZDDH) {
        String tableName = pipeMapper.getTableNameByCode(StrUtil.format("{}_LINE", pipeType).toUpperCase(Locale.ROOT));
        if (StrUtil.isEmpty(tableName)) {
            throw new ServiceException("数据源不存在");
        }
        return pipeMapper.getPipeLineJsonByQddhAndZddh(tableName, QDDH, ZDDH);
    }
}
